<style lang="scss" scoped>
.cart-item {
  width: 100%;
  background-color: #fff;
  margin-top: 0.5rem;
  border-radius: 0.5rem;
  font-size: 0.9rem;
}
.chanked {
  width: 100%;
  display: flex;
  height: 3rem;
  position: relative;
}
.cart-i {
  width: 100%;
  display: flex;
  // background-color: aqua;
  border: 1px solid #f6f6f6;
  img {
    width: 80px;
  }
  div:first-child {
    width: 2rem;
    // background-color: red;
  }
  div:last-child {
    flex: 0.5rem;
  }
  div {
    margin-top: 0.3rem;
    margin: 0.5rem;
  }
}
.cart-i:last-child {
  border-bottom: none;
}
.cart-i-right {
  p {
    font-size: 0.8rem;
    color: #626262;
    margin-top: 0.3rem;

    button {
      height: 1rem;
      line-height: 1rem;
      background-color: #4dd676;
      border: none;
      // padding: 0 .2rem;
      color: #fff;
      border-radius: 15%;
      margin-right: 0.2rem;
    }
  }
}
</style>
<template>
  <div
    style="
      background-color: #f2f2f2;
      width: 100wv;
      height: 100vh;
      overflow: auto;
      padding-bottom: 5rem;
    "
  >
    <van-nav-bar
      title="购物车"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      right-text="编辑删除"
      @click-right="onClickRight"
    />
    <div style="width: 97%">
      <div v-for="(item, index) in cart" :key="index" class="cart-item">
        <div class="chanked" @click="cartChanked(item)">
          <van-checkbox
            v-model="item.checked"
            style="position: absolute; top: 1rem; left: 0.6rem; font-size: 1rem"
            icon-size="1.2rem"
            checked-color="#ff6134"
            >{{ item.title }}</van-checkbox
          >
        </div>
        <div>
          <div
            v-for="(i, index) in item.goods"
            :key="index"
            class="cart-i"
            @click="allchanked(i, item)"
          >
            <div style="display: flex; align-items: center">
              <van-checkbox
                v-model="i.checked"
                icon-size="1rem"
                checked-color="#ff6134"
              ></van-checkbox>
            </div>
            <div style="flex: 0.3">
              <img :src="i.image" alt="" />
            </div>
            <div class="cart-i-right">
              <p>
                <button>{{ i.tag }}</button>{{ i.title }}
              </p>
              <select
                style="
                  background-color: #f2f2f2;
                  border: none;
                  color: #c1c1c1;
                  width: 80%;
                  padding: 0.1rem 0;
                  margin-top: 0.8rem;
                "
              >
                <option>{{ i.title }}</option>
                <option>200丸/瓶</option>
                <option>0.29克*/54片</option>
              </select>
              <div style="display: flex; margin-top: 0.5rem; width: 100%; align-items: center">
                <p style="flex: 0.9; color: red">￥{{ i.price }}</p>
                <van-stepper v-model="i.num" input-width="1.5rem" button-size="1.5rem" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <van-empty description="暂无任何数据" v-if="!cart.length" />

    <van-submit-bar
      button-color="#ff6700"
      :price="toAllPrice * 100"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <div><van-checkbox @change="onClickCheckAll" v-model="allchecked">全选</van-checkbox></div>
      <template #tip> 你的收货地址不支持配送, <span @click="onClickLink">修改地址</span> </template>
    </van-submit-bar>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
const onClickLeft = () => history.back()
var cart = ref(JSON.parse(localStorage.getItem('cartData') as string) || [])
const router = useRouter()
const onClickLink = () => showToast('修改地址')
const allchecked = ref(JSON.parse(localStorage.getItem('allchecked') as string) || false)
const onClickRight = () => {
  // var data = cart.value.filter((item: any) => item.checked == true)
  // cart.value.splice(data, 1)
  // localStorage.setItem('cartData', JSON.stringify(cart.value))
  cart.value.forEach((item: any) => {
    item.goods.forEach((v: any) => {
      if (v.checked == true) {
        item.goods.splice(v, 1)
      }
    })
  })
}
const onClickCheckAll = (value: any) => {
  cart.value.forEach((item: any) => {
    item.checked = value
    item.goods.forEach((item: any) => {
      item.checked = value
    })
  })
  localStorage.setItem('cartData', JSON.stringify(cart.value))
  localStorage.setItem('allchecked', JSON.stringify(allchecked.value))
}
const cartChanked = (item: any) => {
  console.log(item)
  item.checked = !item.checked
  if (item.checked) {
    item.goods.forEach((item: any) => {
      item.checked = true
    })
  } else {
    item.goods.forEach((item: any) => {
      item.checked = false
    })
  }
}
// 计算总价
const toAllPrice = computed(() => {
  var zprice = 0
  cart.value.forEach((item: any) => {
    item.goods.forEach((item: any) => {
      if (item.checked) {
        zprice += item.price * item.num
      }
    })
  })
  return zprice
})
const allchanked = (item: any, ind: any) => {
  item.checked = !item.checked
  var num = 0
  ind.goods.forEach((item1: any) => {
    if (!item1.checked) {
      ind.checked = false
    } else if (item1.checked) {
      num++
      if (num == ind.goods.length) {
        ind.checked = true
      }
    }
  })
  console.log(ind)
}
const onSubmit = () => {
  console.log(cart.value)
  var data: any = []
  cart.value.forEach((item: any) => {
    item.goods.forEach((i: any) => {
      if (i.checked) {
        data.push({ title: i.vendor, goods: [i] })
      }
    })
  })
  const mergedArr = Object.values(
    data.reduce((acc: any, current: any) => {
      const key = current.title
      if (!acc[key]) {
        acc[key] = { ...current }
      } else {
        acc[key].goods.push(current.goods[0])
      }
      return acc
    }, {}),
  )

  router.push({
    path: '/order',
    query: { data: JSON.stringify(mergedArr) },
  })
}
</script>
